<template>
  <div id="RoutineDetails">
    <v-tableTitle
      :btnConfig="btnConfig"
      @closeSave="closeSave"
      @save="save"
    ></v-tableTitle>
    <v-detail
      :detailData="inputData.detailDataFpkw"
      :inputConfig="inputConfig"
      :rules="rules"
      ref="input"
      @pop="setdata"
    ></v-detail>
  </div>
</template>

<script>
import detail from "commons/Detail";
import baseMixin from "@/mixins/BaseMixin";
import tableTitle from "@/components/commons/TableTitle";

export default {
  name: "replacedetail",
  mixins: [baseMixin],
  props: {
    inputData: {
      type: Object,
    },
  },
  data() {
    return {
      rules: {
        substitutesku: [
          { required: true, validator: this.substitutesku, trigger: "blur" },
        ],
        qty: [{ required: true, validator: this.qty, trigger: "blur" }],
        subqty: [{ required: true, validator: this.subqty, trigger: "blur" }],
      },
      whseidflag: true,
    };
  },
  computed: {
    btnConfig() {
      return [
        { label: this.$t("common.save"), invalid: "save" },
        { label: this.$t("common.cancel"), invalid: "closeSave" },
      ];
    },
    inputConfig() {
      if (this.whseidflag) {
        return [
          [
            {
              type: "input",
              label: this.$t("config.shipper"),
              props: "storerkey",
              disabled: true,
            },
            {
              type: "input",
              label: this.$t("config.sku"),
              props: "sku",
              disabled: true,
            },
            {
              type: "input",
              label: this.$t("outbound.replaceGoods"),
              disabled: true,
              props: "substitutesku",
            },
          ],
          [
            {
              type: "input",
              label: this.$t("config.sequence"),
              props: "sequence",
              disabled: true,
            },
            {
              type: "input",
              label: this.$t("outbound.qty"),
              props: "qty",
              disabled: true,
            },
            {
              type: "input",
              label: this.$t("outbound.subqty"),
              props: "subqty",
              disabled: true,
            },
          ],
        ];
      } else {
        return [
          [
            {
              type: "input",
              label: this.$t("config.shipper"),
              props: "storerkey",
              disabled: true,
            },
            {
              type: "input",
              label: this.$t("config.sku"),
              props: "sku",
              disabled: true,
            },
            /* {
                      type: "input",
                      label: this.$t('outbound.replaceGoods'),
                      props: "substitutesku",
                   },*/
            {
              type: "table",
              label: this.$t("outbound.replaceGoods"),
              disabled: !this.$parent.fpkwFlag,
              props: "substitutesku",
              tableConfig: [
                {
                  label: this.$t("config.sku"),
                  prop: "substitutesku",
                  search: "sku",
                  input: "input",
                },
                {
                  label: this.$t("common.desc"),
                  prop: "descr",
                  input: "input",
                },
                {
                  label: this.$t("config.shipper"),
                  prop: "storerkey",
                  input: "input",
                },
                {
                  label: this.$t("config.packaging"),
                  prop: "packkey",
                  input: "input",
                },
              ],
              url:
                "querySubstituteSkuOfStorerkey/?/wmwhse1/" +
                this.inputData.detailDataFpkw.storerkey +
                "/" +
                this.inputData.detailDataFpkw.sku,
            },
            
          ],
          [
            {
              type: "input",
              label: this.$t("config.sequence"),
              props: "sequence",
            },
            {
              type: "input",
              label: this.$t("outbound.qty"),
              props: "qty",
            },
            
            {
              type: "input",
              label: this.$t("outbound.subqty"),
              props: "subqty",
            },
          ],
        ];
      }
    },
  },
  methods: {
    save() {
      let url = "";
      let data = {
        ...this.inputData.detailDataFpkw,
      };
      if (this.$parent.fpkwFlag) {
        url = "/insertSubstitutesku";
      } else {
        url = "/updateSubstitutesku";
      }
      this.$api.post(url, data).then((res) => {
          this.$message.success(res.returnMgr);
          this.closeSave();
        
      });
    },
    qty(rule, value, callback) {
      if (value === "") {
        callback();
      } else {
        var re = /^[0-9]*$/;
        let te = value;
        let flag = re.test(te);
        if (flag) {
          if (value < 0) {
            this.$message.error(this.$t("app.inputGreaterZeroNumberPlease"));
            this.data.detailData[rule.field] = "";
            callback(new Error());
          } else {
            callback();
          }
        } else {
          this.$message.error(this.$t("app.inputGreaterZeroNumberPlease"));
          this.data.detailData[rule.field] = "";
          callback(new Error());
        }
      }
    },
    subqty(rule, value, callback) {
      if (value === "") {
        callback();
      } else {
        var re = /^[0-9]*$/;
        let te = value;
        let flag = re.test(te);
        if (flag) {
          if (value < 0) {
            this.$message.error(this.$t("app.inputGreaterZeroNumberPlease"));
            this.data.detailData[rule.field] = "";
            callback(new Error());
          } else {
            callback();
          }
        } else {
          this.$message.error(this.$t("app.inputGreaterZeroNumberPlease"));
          this.data.detailData[rule.field] = "";
          callback(new Error());
        }
      }
    },
    substitutesku(rule, value, callback) {
      if (value === "") {
        callback(new Error());
      } else {
        this.$api
          .get(
            `/sku/isExist/${value}/${this.$store.state.wms}/${this.inputData.detailDataFpkw.storerkey}`
          )
          .then((res) => {
            
              if (res.bean) {
                if (this.data.detailData.sku == value) {
                  this.$message.error(this.$t('msg.msg44'));
                  callback(new Error());
                } else {
                  callback();
                }
              } else {
                this.$message.error(this.$t('msg.msg44') + `:${value}`);
                callback(new Error());
              }
          })
          .catch((e) => {
            this.$message.error(e);
            callback(new Error());
          });
      }
    },
    setdata(data, id, close) {
      if (id == "2") {
        this.inputData.detailDataFpkw.substitutesku = data.substitutesku;
      }
      close(false);
    },
    closeSave() {
      this.$parent.setMentActive("11");
    },
    getData() {},
  },
  mounted() {
    console.log(this.inputData);
    if (this.$store.state.wms.toUpperCase().includes("WMWHSE")) {
      //仓库级
      this.whseidflag = false;
    }
  },
  watch: {},
  components: {
    "v-detail": detail,
    "v-tableTitle": tableTitle,
  },
};
</script>

<style scoped></style>
